<?php
namespace app\manage\controller;

use app\model\{da}Model;
use app\manage\controller\Base;
use support\Request;
use think\facade\Db;
use support\View;
class {da}Controller extends Base
{

    public function index(Request $request)
    {
    	$key=$request->input('key','');
    	View::assign('key',$key);
		${xiao}M=new {da}Model();
		${xiao}=${xiao}M{con_cha}->paginate(['list_rows'=>10,'query'=>request()->all()])->each(function($item, $key){
			//$item->nickname = 'think';
		});
		View::assign('{xiao}',${xiao});
		View::assign('count1', ${xiao}->total());
       	return view('{xiao}/index');
    }
	public function ajax(Request $request)
    {
    	$data=$request->all();
		${xiao}=new {da}Model();
		if($data['type']=='{xiao}_del'){
			$info=${xiao}->destroy($data['id']);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='{xiao}_sort'){
			$arrlength=count($data['id']);
			$ar=[];
			for($x=0;$x<$arrlength;$x++)
			{
			    $ar[$x]=['id'=>$data['id'][$x], 'sort'=>$data['sort'][$x]];
			}
			$info=${xiao}->saveAll($ar);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='{xiao}_start'){
		    if(${xiao}->where('id',$data['id'])->update(['isopen'=>1])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='{xiao}_stop'){
		    if(${xiao}->where('id',$data['id'])->update(['isopen'=>0])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		return json(['code' => 0, 'message' => '失败']);
    }
	public function add(Request $request)
    {
    	if(request()->method()=='POST'){
			$data=$request->post();
			$validate = new \app\manage\validate\{da}Validate;
			if(!$validate->check($data)){
				return view_e($validate->getError());
			}	
			{file}
            {date_zhuan}
			${xiao}=new {da}Model();
			$res=${xiao}->save($data);
			if($res){
				return view_s('添加成功',url('{xiao}/index',['st'=>1]));
			}else{
				return view_e('添加失败');
			}
    	}
		return view('{xiao}/add');
    }
	public function edit(Request $request)
    {
		$id=$request->input('id',0);
		${xiao} = new {da}Model();
    	if(request()->method()=='POST'){
    		$data=$request->post();
			$validate = new \app\manage\validate\{da}Validate;
			if(!$validate->check($data)){
				return view_e($validate->getError());
			}	
			{edit_file}
            {date_zhuan}
			$res=${xiao}->where('id', $id)->update($data);
			if($res){
				return view_s('修改成功',url('{xiao}/index',['st'=>1]));
			}else{
				return view_e('修改失败');
			}
    	}
		$data= ${xiao}->where('id',$id)->find();
		View::assign('data',$data);
		return view('{xiao}/edit');
    }
}
